package com.bawei.park2211a.car

import android.app.Notification
import android.app.NotificationManager

import androidx.appcompat.widget.SearchView

import androidx.core.view.GravityCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.bean.CarEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.park2211a.R
import com.bawei.park2211a.adapter.CarAdapter
import com.bawei.park2211a.databinding.ActivityCarBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/car/CarActivity")
class CarActivity : BaseActivity<ActivityCarBinding, CarViewModel>() {
   private lateinit var carAdapter: CarAdapter
  private  val list = mutableListOf<CarEntity>()

    override fun initView() {

        viewModel.car(1)
        carAdapter = CarAdapter()
        binding.carRec.adapter = carAdapter
        binding.carRec.layoutManager = LinearLayoutManager(this)
        binding.carSearch.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                carAdapter.data.clear()
                list.forEach {
                    if (it.spaceStatus == "已占用")
                        if (it.licensePlate.contains(query!!)) {
                            carAdapter.data.add(it)
                        }
                }
                carAdapter.notifyItemRangeChanged(0, carAdapter.data.size)
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText!!.isEmpty()) {
                    carAdapter.data.clear()
                    carAdapter.data.addAll(list)
                    carAdapter.notifyItemRangeChanged(0, carAdapter.data.size)
                }
                return true
            }

        })
        carAdapter.setOnItemClickListener { _, _, position ->
            //创建通知
            val build = Notification.Builder(this)
            build.setSmallIcon(R.mipmap.ic_launcher)
            build.setContentTitle("车位信息")
            build.setContentText("车位号：${carAdapter.data[position].spaceNumber}")
            build.setAutoCancel(true)
            val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.notify(1, build.build())
        }

        binding.type.setOnClickListener {
            binding.linDrawer.openDrawer(GravityCompat.START)
        }
        binding.parkAdd.setOnClickListener {
            ARouter.getInstance().build("/app/car/AddCarActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.carSuccess.observe(this) {
            list.addAll(it)
            carAdapter.setNewData(it)
            carAdapter.notifyItemRangeChanged(0, it.size)
        }
    }
}